#include "stdafx.h"
#include "ShaderAmbient.h"

#include "DrawCall.h"
#include "RenderingData.h"
#include "Model.h"
#include "Material.h"

using namespace mgfx;

ShaderAmbient::ShaderAmbient()
{
	mVar_gDiffuseMap = NULL;
	mVar_gWVP = NULL;
	mVar_gAmbLight = NULL;
	mVar_gDiffMat = NULL;

	mFilename = L"Ambient.fx";
}

bool ShaderAmbient::init()
{
	Tech = Effect->GetTechniqueByName("tech");
	mVar_gDiffuseMap = Effect->GetVariableByName("gDiffuseMap")->AsShaderResource();
	mVar_gWVP = Effect->GetVariableByName("gWVP")->AsMatrix();
	mVar_gDiffMat = Effect->GetVariableByName("gDiffMat")->AsVector();
	mVar_gAmbLight = Effect->GetVariableByName("gAmbLight")->AsVector();
	
	if(!Tech || ! mVar_gDiffuseMap || !mVar_gWVP)
		return false;
	else
	{
		mInitialized = true;
		return true;
	}
}

void ShaderAmbient::setParamsGlobal()
{
	RenderingData & data = RenderingData::Instance;
	mVar_gAmbLight->SetFloatVector(data.Pointlight.Ambient);
}

void ShaderAmbient::setParamsModel(ModelDrawCall * drawCall)
{
	//XMMATRIX world = XMLoadFloat4x4(&drawCall->WorldMatrix);
	XMMATRIX world = drawCall->WorldMatrix;
	XMMATRIX viewProj = XMLoadFloat4x4(&RenderingData::Instance.ViewProj);
	world = XMMatrixMultiply(world, viewProj);

	//D3DXMATRIX wvp;
	//D3DXMatrixMultiply(&wvp, &drawCall->WorldMatrix, &RenderingData::Instance.ViewProj);

	mVar_gWVP->SetMatrix((float*)&world);

	Material * mat = drawCall->Model->getMaterial();
	mVar_gDiffuseMap->SetResource(mat->DiffuseMap);
	mVar_gDiffMat->SetFloatVector(mat->DiffuseColor);
}